// Library.java
import java.time.LocalDate;
import java.util.*;

public class Library {
    private Map<String, Book> books;
    private Map<String, User> users;
    private List<String> categories;

    public Library() {
        books = new HashMap<>();
        users = new HashMap<>();
        categories = new ArrayList<>(Arrays.asList("小说", "科技", "历史", "艺术", "教育", "传记", "文学", "其他"));
    }

    // 图书管理
    public void addBook(Book book) {
        books.put(book.getIsbn(), book);
    }

    public void removeBook(String isbn) {
        books.remove(isbn);
    }

    public Book findBook(String isbn) {
        return books.get(isbn);
    }

    // 用户管理
    public void addUser(User user) {
        users.put(user.getUserId(), user);
    }

    public void removeUser(String userId) {
        users.remove(userId);
    }

    public User findUser(String userId) {
        return users.get(userId);
    }

    // 借阅管理
    public boolean borrowBook(String userId, String isbn) {
        User user = users.get(userId);
        Book book = books.get(isbn);

        if (user == null || book == null || !book.isAvailable()) {
            return false;
        }

        book.setAvailable(false);
        book.setDueDate(LocalDate.now().plusWeeks(2)); // 借阅期限2周
        book.setBorrowedBy(userId); // 记录借阅者
        user.borrowBook(book);
        return true;
    }

    public boolean returnBook(String userId, String isbn) {
        User user = users.get(userId);
        Book book = books.get(isbn);

        if (user == null || book == null || book.isAvailable()) {
            return false;
        }

        book.setAvailable(true);
        book.setBorrowedBy(""); // 清除借阅者信息
        user.returnBook(book);
        return true;
    }

    // 搜索功能
    public List<Book> searchByTitle(String title) {
        List<Book> result = new ArrayList<>();
        for (Book book : books.values()) {
            if (book.getTitle().toLowerCase().contains(title.toLowerCase())) {
                result.add(book);
            }
        }
        return result;
    }

    public List<Book> searchByAuthor(String author) {
        List<Book> result = new ArrayList<>();
        for (Book book : books.values()) {
            if (book.getAuthor().toLowerCase().contains(author.toLowerCase())) {
                result.add(book);
            }
        }
        return result;
    }

    public List<Book> searchByCategory(String category) {
        List<Book> result = new ArrayList<>();
        for (Book book : books.values()) {
            if (book.getCategory().equalsIgnoreCase(category)) {
                result.add(book);
            }
        }
        return result;
    }

    // 获取所有图书
    public List<Book> getAllBooks() {
        return new ArrayList<>(books.values());
    }

    // 获取所有用户
    public List<User> getAllUsers() {
        return new ArrayList<>(users.values());
    }

    // 获取所有分类
    public List<String> getCategories() {
        return categories;
    }

    // 获取借阅者姓名
    public String getBorrowerName(String isbn) {
        Book book = books.get(isbn);
        if (book != null && !book.isAvailable()) {
            User user = users.get(book.getBorrowedBy());
            if (user != null) {
                return user.getName();
            }
        }
        return "";
    }
}